package com.wwwarehouse.resource_center.fragment.information;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.animation.Animation;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.wwwarehouse.common.activity.base.BaseApplication;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.tools.LogUtils;
import com.wwwarehouse.resource_center.R;
import com.wwwarehouse.resource_center.bean.goods.PaginationItemOrResourceBean;
import com.wwwarehouse.resource_center.constant.ResourceConstant;
import com.wwwarehouse.resource_center.fragment.rules.TheParentFragment;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Joseph on 2017/9/25.
 *         <p/>
 *         创建物和资源列表界面
 */

public class GoodsInfomationListFragment extends TheParentFragment {

    ListView lvlist;
    List<PaginationItemOrResourceBean.ListBean> list;
    InfomationListAdaper listAdaper ;
    int num = 7;    //每一页的个数
    int pos;        //当前页数
    int count;      //总共几页
    int mItemHeight; //每一个条目的高度
    String type ;
    public static final int PAGINATION_ITEMORRESOURCE = 2;  //

    @Override
    protected int getContentId() {
        return R.layout.fragment_infomation_list;
    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }

    @Override
    protected void init(View view) {
        lvlist = findView(view, R.id.lv_list);
    }

    @Override
    protected void loadDatas() {
        list = new ArrayList<>();
        try {
            Bundle b = getArguments();
            pos = b.getInt("pos");
            num = b.getInt("num");
            type = b.getString("type");
            count = b.getInt("count");
        } catch (Exception e) {
            e.printStackTrace();
        }
//        mActivity.setTitle(type.equals("1")?getString(R.string.infomation_create_item):getString(R.string.infomation_create_resources));
        reques();
    }

    private void reques() {
        Map map = new HashMap();
        Map<String, Object> query = new HashMap<>();
//        map.put("businessId", sp.getValue("businessId"));     //新接口文档没有
        map.put("itemType", "");//按分类查询物或资源，非必须
        map.put("name", "");                 //按名称模糊查询，非必须
        map.put("type", type);   // 物：1、资源：0，必须
        query.put("page", pos);
        query.put("size", num);
        query.put("sort", "");
        map.put("query", query);
        httpPost(ResourceConstant.PAGINATION_ITEMORRESOURCE, map, PAGINATION_ITEMORRESOURCE, false, "");
    }

    @Override
    public void setTitle() {

    }

    private class InfomationListAdaper extends BaseAdapter {

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final ViewHodler viewHodler;
            if (convertView == null) {
                viewHodler = new ViewHodler();
                convertView = LayoutInflater.from(mActivity).inflate(R.layout.adapter_goods_infomation, parent, false);
                ViewTreeObserver vto = lvlist.getViewTreeObserver();
                final View finalConvertView = convertView;
                vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        lvlist.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                        LogUtils.showInfoLog("num:"+num);
                        ViewGroup.LayoutParams layoutParams = finalConvertView.getLayoutParams();
                        mItemHeight = lvlist.getHeight() / num;
                        layoutParams.height = mItemHeight;
                        LogUtils.showInfoLog("mItemHeight:"+mItemHeight);
                        finalConvertView.setLayoutParams(layoutParams);
                        LogUtils.showInfoLog("getHeight:"+lvlist.getHeight()+"mItemHeight:"+mItemHeight);
                    }
                });
                viewHodler.mName = (TextView) convertView.findViewById(R.id.goods_name);
                viewHodler.typeName = (TextView) convertView.findViewById(R.id.goods_category);
                viewHodler.aLineName = (TextView) convertView.findViewById(R.id.a_line_goods_name);
                viewHodler.aLine = (LinearLayout) convertView.findViewById(R.id.a_line);
                viewHodler.twoLine = (LinearLayout) convertView.findViewById(R.id.two_line);
                viewHodler.mImg = (ImageView) convertView.findViewById(R.id.img);
                convertView.setTag(viewHodler);
            } else {
                viewHodler = (ViewHodler) convertView.getTag();
            }
            viewHodler.twoLine.setVisibility(View.VISIBLE);
              viewHodler.aLine.setVisibility(View.GONE);
            viewHodler.mName.setText(list.get(position).getName());
            viewHodler.typeName.setText(list.get(position).getTypeName());
//            String typeCode = list.get(position).getTypeCode();
//            switch (typeCode){
//                case "H_SHOP" :
//                case "H_STOCK":
//                    viewHodler.twoLine.setVisibility(View.VISIBLE);
//                    viewHodler.aLine.setVisibility(View.GONE);
//                    viewHodler.mName.setText(list.get(position).getName());
//                    viewHodler.typeName.setText(list.get(position).getTypeName());
//                    break;
//                case "H_GOODS" :
//                case "H_TOOLS"  :
//                case "H_SALE"  :
//                case "H_STORE" :
//                case "H_LABOUR":
//                case "H_PROCESS" :
//                case "H_SELL"  :
//                case "H_TRANSPORT" :
////                    viewHodler.aLineName.setText(list.get(position).getName());
//                    break;
//                default:
//                    break;
//            }

            if (TextUtils.isEmpty(list.get(position).getPicUrl())){
                viewHodler.mImg.setBackgroundResource(R.drawable.picture_no);
            }else {
                ImageLoader.getInstance().displayImage(list.get(position).getPicUrl(), viewHodler.mImg, new ImageLoadingListener() {
                    @Override
                    public void onLoadingStarted(String s, View view) {

                    }

                    @Override
                    public void onLoadingFailed(String s, View view, FailReason failReason) {
                        viewHodler.mImg.setBackgroundResource(R.drawable.picture_no);
                    }

                    @Override
                    public void onLoadingComplete(String s, View view, Bitmap bitmap) {

                    }

                    @Override
                    public void onLoadingCancelled(String s, View view) {

                    }
                });
            }
            convertView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    /**
                     * 物的分类：H_GOODS(商品)、H_TOOLS(工具)、H_SHOP(店铺)、H_STOCK(仓库)、
                     * 资源的分类：H_SALE(供货)、H_STORE(仓储)、H_LABOUR(劳务)、H_PROCESS(加工)、H_SELL(销售)、H_TRANSPORT(运输)
                     */
                    String typeCode = list.get(position).getTypeCode();
                    String  type = "1" ;
                    Fragment  fragment = null;
                    switch (typeCode){
                        case "H_GOODS" :
                            fragment = new InfomationGoodsFragment();
                            break;
                        case "H_TOOLS" :
                            fragment = new InfomationToolsFragment();
                            break;
                        case "H_SHOP" :
                            fragment = new InfomationShopFragment();
                            break;
                        case "H_STOCK" :
                            fragment = new InfomationWarehouseFragment();
                            break;
                        case "H_SALE" :
                        case "H_STORE" :
                        case "H_LABOUR" :
                        case "H_PROCESS" :
                        case "H_SELL" :
                        case "H_TRANSPORT" :
                            type = "0" ;
                            fragment = new InfomationResourcesFragment();
                            break;
                        default:
                            type = "0" ;
                            fragment = new InfomationResourcesFragment();
                            break;
                    }
                    Bundle  bundle = new Bundle() ;
                    bundle.putString("itemType",typeCode);
                    bundle.putLong("ukid",list.get(position).getUkid());
                    bundle.putString("type",type);
                    bundle.putString("typeName",list.get(position).getTypeName());
//                    bundle.putInt("pos", pos);       //当前第几页
//                    bundle.putInt("count", count);   //总页数
//                    bundle.putInt("num", num);       //每一页个数
//                    bundle.putInt("num", num);       //当前页数的第几条数据
                    fragment.setArguments(bundle);
                    pushFragment(fragment);
                }
            });
            return convertView;
        }

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }
    }

    class ViewHodler {
        TextView mName;
        TextView typeName;
        ImageView mImg;
        TextView aLineName;
        LinearLayout twoLine ;
        LinearLayout aLine ;
    }

    @Override
    public void onSuccess(CommonClass commonClass, int what) {
        switch (what){
            case PAGINATION_ITEMORRESOURCE:
                if (TextUtils.equals("0", commonClass.getCode())) {
                    PaginationItemOrResourceBean goodsSelectLabelBean = JSON.parseObject(commonClass.getData().toString(), PaginationItemOrResourceBean.class);
                    list = goodsSelectLabelBean.getList();
                    listAdaper = new InfomationListAdaper();
                    lvlist.setAdapter(listAdaper);
                }
                break;
        }

    }

    @Override
    public void onReLoad(int code) {
        reques();
    }
}
